'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

exports.makeActionType = makeActionType;
exports.makeCallAPI = makeCallAPI;
exports.makeBaseApi = makeBaseApi;
exports.makeBaseActionType = makeBaseActionType;
exports.bindAction = bindAction;

var _redux = require('redux');

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _api = require('../middlewares/api');

var _mustache = require('mustache');

var _mustache2 = _interopRequireDefault(_mustache);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 * generate RSAA three action type
 * 生成RSAA标准的三个action状态
 * @param  {[string]} type [the action indetifier, pls UpperCase before you call this method]
 * @return {[object]}      [object with three action types]
 */
function makeActionType(type) {
	var upper = _lodash2.default.toUpper(type).trim();
	var result = {
		request: upper + "_REQUEST",
		success: upper + "_SUCCESS",
		fail: upper + "_FAILURE"
	};
	result.types = _lodash2.default.values(result);
	return result;
}

/**
 * generate RSAA standard action for user
 * 生成RSAA标准action
 * @param  {[object]} options [RSAA options]
 * @return {[object]}         [reduxh dispatchable action]
 */
function makeCallAPI(options) {
	return function (body) {
		console.log('call api');
		if (body && (body.adds || _lodash2.default.isNumber(body.adds))) {
			//request with adds
			options.body = body.body;
			options.adds = body.adds;
		} else if (body) {
			if (body) options.body = body;
		}
		return _defineProperty({}, _api.CALL_API, options);
	};
}

/**
 * automatic generate basic action(CRUD)
 * 自动生成 CRUD action
 * @param  {[string]} type [the action indetifier]
 * @param  {[string]} url  [url to interact]
 * @return {[object]}      [action body]
 */
function makeBaseApi(type, url) {
	var BASE_TYPE_NAME = _lodash2.default.toUpper(type).trim();
	var types = makeBaseActionType(type);

	var isTemplate = url.indexOf("{{") == -1 ? false : true;

	var getReq = function getReq(mappingObj) {
		return makeCallAPI({
			endpoint: isTemplate ? _mustache2.default.render(url, mappingObj) : url,
			method: 'GET',
			types: types.getType.types
		})();
	};

	var postReq = function postReq(mappingObj, data) {
		return makeCallAPI({
			endpoint: isTemplate ? _mustache2.default.render(url, mappingObj) : url,
			method: 'POST',
			types: types.postType.types
		})(data);
	};

	var getOneReq = function getOneReq(mappingObj) {
		return makeCallAPI({
			endpoint: isTemplate ? _mustache2.default.render(url, mappingObj) : url,
			method: 'GET',
			types: types.getType.types
		})();
	};

	var deleteReq = function deleteReq(mappingObj, meta) {
		return makeCallAPI({
			endpoint: isTemplate ? _mustache2.default.render(url, mappingObj) : url,
			method: 'DELETE',
			meta: meta,
			types: types.deleteType.types
		})();
	};

	var putReq = function putReq(mappingObj, data) {
		return makeCallAPI({
			endpoint: isTemplate ? _mustache2.default.render(url, mappingObj) : url,
			method: 'PUT',
			types: types.putType.types
		})(data);
	};

	var result = {
		get: getReq,
		getOne: getOneReq,
		put: putReq,
		post: postReq,
		delete: deleteReq,
		_type: 'actionHelper'
	};
	return _extends({}, result, types);
}

/**
 * generate CRUD action type
 * 生成CRUD action
 * type structure
 * {
 * 	[your uppercased type string]+"_GET/_POST/.etc": [REAL_TYPE] (obj with three types)
 * }
 *
 * @param  {[type]} type [description]
 * @return {[object]}      [return object ]
 */
function makeBaseActionType(type) {
	var BASE_TYPE_NAME = _lodash2.default.toUpper(type).trim();

	var getStr = BASE_TYPE_NAME + "_GET";
	var getOneStr = BASE_TYPE_NAME + "_GET_ONE";
	var postStr = BASE_TYPE_NAME + "_POST";
	var deleteStr = BASE_TYPE_NAME + "_DELETE";
	var putStr = BASE_TYPE_NAME + "_PUT";

	var GET_TYPE = makeActionType(getStr);
	var GET_ONE_TYPE = makeActionType(getOneStr);
	var POST_TYPE = makeActionType(postStr);
	var DELETE_TYPE = makeActionType(deleteStr);
	var PUT_TYPE = makeActionType(putStr);

	return {
		getType: GET_TYPE,
		getOneType: GET_ONE_TYPE,
		postType: POST_TYPE,
		deleteType: DELETE_TYPE,
		putType: PUT_TYPE
	};
}

/**
 *	bindAction with generated CRUD actions for page/components
 *	为 页面/组件 绑定包含 makeDefaultApi 生成的CRUD的action
 * @param  {[type]} actions  [description]
 * @param  {[type]} dispatch [description]
 * @return {[type]}          [description]
 */
function bindAction(actions, dispatch) {
	var injectObj = {};
	for (var key in actions) {
		if (actions[key]._type) {
			injectObj = _extends({}, injectObj, actions[key]);
		}
	}
	var newAction = _extends({}, actions, injectObj);
	return (0, _redux.bindActionCreators)(newAction, dispatch);
}

// /**
//  * @TODO
//  * [generateBaseReducer description]
//  * @param  {[type]} initialState [description]
//  * @param  {[type]} key          [description]
//  * @return {[type]}              [description]
//  */
// export function makeBaseReducer(initialState,type){
// 	const BASE_TYPE_NAME = _.toUpper(type).trim();
//
// 	const types = makeBaseActionType(type);
//
// 	var options = {}
//
// 	for(var key in types){
// 		optioins[types[key].success] = (state, action) => {
// 			if(!action.payload){
// 				console.error('no payload in action, check whether your response is correct');
// 				return state;
// 			}
// 			if(typeof action.payload === 'object'){
// 				return action.payload;
// 			}
// 		}
// 	}
//
//   return createReducer(initialState,{
// 		[GET_TYPE]:function(state, action){
//
// 		}
//   })
// }